package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.ADUser;
import com.vne.skyemart.service.UserService;

@Controller
@SessionAttributes
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveUser(@ModelAttribute(" user") ADUser user,
			BindingResult result) {
		userService.addUser(user); 
		return new ModelAndView("redirect:/user.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listUser(Model model) {
		model.addAttribute("user",  userService.listUser());

		return "userList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addUser(@ModelAttribute("user") ADUser user,
			BindingResult result) {
		return new ModelAndView("addUser");
	}

}